//  代码地址: https://github.com/CoderLLLee/LLRefresh
//  代码地址: http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0/52326ce26803fabc46000000
//  UIScrollView+LLRefresh.m
//  LLRefreshExample
//
//  Created by LL Lee on 15/3/4.
//  Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "UIScrollView+LLRefresh.h"
#import <objc/runtime.h>
#import "LLRefreshComponent.h"
#import "LLRefreshNomalHeader.h"
#import "LLRefreshNomalFooter.h"

@implementation NSObject (LLRefresh)

+ (void)exchangeInstanceMethod1:(SEL)method1 method2:(SEL)method2 {
    method_exchangeImplementations(class_getInstanceMethod(self, method1), class_getInstanceMethod(self, method2));
}

+ (void)exchangeClassMethod1:(SEL)method1 method2:(SEL)method2 {
    method_exchangeImplementations(class_getClassMethod(self, method1), class_getClassMethod(self, method2));
}

@end

@implementation UIScrollView (LLRefresh)

#pragma mark - header
static const char LLRefreshHeaderKey = '\0';

- (void)setHeader:(LLRefreshComponent *)header {
    if (header != self.header) {
        // 删除旧的，添加新的
        [self.header removeFromSuperview];
        [self addSubview:header];

        // 存储新的
        [self willChangeValueForKey:@"header"]; // KVO
        objc_setAssociatedObject(self, &LLRefreshHeaderKey,
                header, OBJC_ASSOCIATION_ASSIGN);
        [self didChangeValueForKey:@"header"]; // KVO
    }
}

- (LLRefreshComponent *)header {
    return objc_getAssociatedObject(self, &LLRefreshHeaderKey);
}

#pragma mark - footer
static const char LLRefreshFooterKey = '\0';

- (void)setFooter:(LLRefreshComponent *)footer {
    if (footer != self.footer) {
        // 删除旧的，添加新的
        [self.footer removeFromSuperview];
        [self addSubview:footer];

        // 存储新的
        [self willChangeValueForKey:@"footer"]; // KVO
        objc_setAssociatedObject(self, &LLRefreshFooterKey,
                footer, OBJC_ASSOCIATION_ASSIGN);
        [self didChangeValueForKey:@"footer"]; // KVO
    }
}

- (LLRefreshComponent *)footer {
    return objc_getAssociatedObject(self, &LLRefreshFooterKey);
}

#pragma mark - other

- (NSInteger)totalDataCount {
    NSInteger totalCount = 0;
    if ([self isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *) self;

        for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
            totalCount += [tableView numberOfRowsInSection:section];
        }
    } else if ([self isKindOfClass:[UICollectionView class]]) {
        UICollectionView *collectionView = (UICollectionView *) self;

        for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
            totalCount += [collectionView numberOfItemsInSection:section];
        }
    }
    return totalCount;
}

static const char LLRefreshReloadDataBlockKey = '\0';

- (void)setReloadDataBlock:(void (^)(NSInteger))reloadDataBlock {
    [self willChangeValueForKey:@"reloadDataBlock"]; // KVO
    objc_setAssociatedObject(self, &LLRefreshReloadDataBlockKey, reloadDataBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self didChangeValueForKey:@"reloadDataBlock"]; // KVO
}

- (void (^)(NSInteger))reloadDataBlock {
    return objc_getAssociatedObject(self, &LLRefreshReloadDataBlockKey);
}

- (void)executeReloadDataBlock {
    !self.reloadDataBlock ?: self.reloadDataBlock(self.totalDataCount);
}
@end

@implementation UITableView (LLRefresh)

+ (void)load {
    [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(LL_reloadData)];
}

- (void)LL_reloadData {
    [self LL_reloadData];
    [self executeReloadDataBlock];
}
@end

@implementation UICollectionView (LLRefresh)

+ (void)load {
    [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(LL_reloadData)];
    [self exchangeInstanceMethod1:@selector(reloadSections:) method2:@selector(LL_reloadSections:)];
    [self exchangeInstanceMethod1:@selector(insertItemsAtIndexPaths:) method2:@selector(LL_insertItemsAtIndexPaths:)];
}

- (void)LL_reloadData {
    [self LL_reloadData];
    [self executeReloadDataBlock];
}

- (void)LL_insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    [self LL_insertItemsAtIndexPaths:indexPaths];
    [self executeReloadDataBlock];
}

- (void)LL_reloadSections:(NSIndexSet *)sections {
    [self LL_reloadSections:sections];
    [self executeReloadDataBlock];
}

@end
